const simpleGit = require('simple-git/promise')
const chalk = require('chalk')
const ora = require('ora')
const path = require('path')
const targetDirPath = process.env.PWD || process.cwd && process.cwd()
const { createGitUrl } = require('../constant')

module.exports = (argv) => {
    const createArgv = argv['c'] || argv['create']
    const gitCloneUrl = typeof createArgv == 'string' ? createArgv : createGitUrl
    const localPath = targetDirPath.split(path.sep).join('/') + '/weapp'

    console.log(chalk.yellow('开始下载远程模板: ' + gitCloneUrl))
    const spinner = ora({
        text: '下载远程模板...',
    })
    spinner.start()
    simpleGit()
        .silent(true)
        .clone(gitCloneUrl, localPath)
        .then(res => {
            spinner.stop()
            console.log(chalk.yellow('模板下载成功, 模板所在目录: ' + localPath))
        }).catch(err => {
            spinner.stop()
            console.log(chalk.red('模板下载失败'), '\n', err)
            process.exit()
        })
}